package com.gitee.ywj1352.数学;

import org.junit.Test;

import java.util.Arrays;

/**
 * java 的基本  左移 右移 | & ^(异或)
 *
 * @author wenjie.yang on 2019/11/12.
 */
public class Demo1 {
    @Test
    public void left() {
        int a = -60;
        //带符号 左移 相当于 除2
        System.out.println(a >> 1);// -30

        //无符号 左移 java int的第一位0为正 1为负数
        //因为2无符号 导致第一位的 符号也跟着左移 导致 负数 变正数
        System.out.println(a >>> 1);// 2147483618
    }

    // 二分法 查找 数组中的单词
    public static boolean search(String[] dictionary, String wordToFind) {
        if (dictionary == null) {
            return false;
        }
        if (dictionary.length == 0) {
            return false;
        }
        int left = 0, right = dictionary.length - 1;
        while (left <= right) {
            int middle = (left + right) / 2;
            if (dictionary[middle].equals(wordToFind)) {
                return true;
            } else {
                if (dictionary[middle].compareTo(wordToFind) > 0) {
                    right = middle - 1;
                } else {
                    left = middle + 1;
                }
            }
        }
        return false;
    }

    @Test
    public void fun() {
        String[] dictionary = {"i", "am", "one", "of", "the", "authors", "in", "geekbang"};
        Arrays.sort(dictionary);
        String wordToFind = "i";
        boolean found = search(dictionary, wordToFind);
        if (found) {
            System.out.println(String.format("找到了单词%s", wordToFind));
        } else {
            System.out.println(String.format("未能找到单词%s", wordToFind));
        }
    }


}
